package com.example.demo.service;

import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.bean.CmsArticle;
import com.example.demo.mapper.CmsArticleMapper;

@Service
public class CmsArticleService {
	
	@Autowired
	private CmsArticleMapper articleMapper;
	
	public void insert(CmsArticle article) {
		if(article.getId()!=null) {
			articleMapper.insert(article);
			System.out.println("有id");
		}else {
			String id = UUID.randomUUID().toString();
			article.setId(id);
			articleMapper.insert(article);
			System.out.println("无id");
		}
	}
	
	public int deleteById(String id) {
		return articleMapper.deleteByPrimaryKey(id);
	}
	
	public void saveOrUpdate(CmsArticle article) {
		if(article.getId()!=null) {
			articleMapper.updateByPrimaryKey(article);
			System.out.println("有id");
		}else {
			String id = UUID.randomUUID().toString();
			article.setId(id);
			articleMapper.updateByPrimaryKey(article);
			System.out.println("无id");
		}
	}
	
	public List<CmsArticle> findAll(){
		return articleMapper.selectAll();
	}
	
	public CmsArticle selectById(String id) {
		return articleMapper.selectByPrimaryKey(id);
	}
	
}
